package com.zhejiangzhengyuan.municipal_common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.*;
import com.zhejiangzhengyuan.municipal_common.entity.dto.RoleDto;
import com.zhejiangzhengyuan.municipal_common.entity.vo.RoleVo;
import com.zhejiangzhengyuan.municipal_common.mapper.*;
import com.zhejiangzhengyuan.municipal_common.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.zhejiangzhengyuan.municipal_common.utils.DateFormatUtil.timeFormat;
/**
 * 角色表 服务实现类
 *
 * @author linju
 * @since 2019-11-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
  @Autowired private RoleMapper roleMapper;
  @Autowired private ActionMapper actionMapper;
  @Autowired private RoleActionMappingMapper roleActionMappingMapper;
  @Autowired private SubjectMapper subjectMapper;
  @Autowired private SubjectRoleMappingMapper subjectRoleMappingMapper;

  // 添加 目前没有固定权限类型无法分出对应的url选择直接角色与权限API关联
  @Override
  public int insertRole(RoleDto dto) {
    QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
    roleQueryWrapper.eq("role_name", dto.getRoleName());
    if (roleMapper.selectOne(roleQueryWrapper) != null) {
      return 1;
    }
    Role role =
        new Role()
            .setRoleName(dto.getRoleName())
            .setRoleDesc(dto.getRoleDesc())
            .setUpdateTime(timeFormat(System.currentTimeMillis()))
            .setCreateTime(timeFormat(System.currentTimeMillis()));
    List<Long> ListId = dto.getIdList();
    if (roleMapper.insert(role) > 0) {
      for (Long id : ListId) {
        Action action = actionMapper.selectById(id);
        RoleActionMapping roleActionMapping = new RoleActionMapping();
        roleActionMapping.setActionId(action.getId());
        roleActionMapping.setRoleId(role.getId());
        roleActionMapping.setCreateTime(timeFormat(System.currentTimeMillis()));
        roleActionMapping.setUpdateTime(timeFormat(System.currentTimeMillis()));
        roleActionMappingMapper.insert(roleActionMapping);
      }
      return 1;
    }
    return 0;
  }

  @Override
  public int updateRole(RoleDto dto) {
    List<Long> ListId = dto.getIdList();
    Role role =
        new Role()
            .setId(dto.getId())
            .setRoleName(dto.getRoleName())
            .setRoleDesc(dto.getRoleDesc())
            .setUpdateTime(timeFormat(System.currentTimeMillis()));

    QueryWrapper<RoleActionMapping> roleActionMappingQueryWrapper = new QueryWrapper<>();
    roleActionMappingQueryWrapper.eq("role_id", role.getId());
    List<RoleActionMapping> roleActionMappings =
        roleActionMappingMapper.selectList(roleActionMappingQueryWrapper);
    for (RoleActionMapping roleActionMapping : roleActionMappings) {
      roleActionMappingMapper.deleteById(roleActionMapping);
    }
    if (roleMapper.updateById(role) > 0) {
      for (Long id : ListId) {
        Action action = actionMapper.selectById(id);
        RoleActionMapping roleActionMapping = new RoleActionMapping();
        roleActionMapping.setActionId(action.getId());
        roleActionMapping.setRoleId(role.getId());
        roleActionMappingMapper.insert(roleActionMapping);
      }
      return 1;
    }
    return 0;
  }

  // 刪除角色
  @Override
  public int deleteRole(Long id) {
    QueryWrapper<RoleActionMapping> wrapper = new QueryWrapper<>();
    QueryWrapper<RoleActionMapping> role_id = wrapper.eq("role_id", id);
    roleActionMappingMapper.delete(role_id);
    return roleMapper.deleteById(id);
  }

  // 查询
  @Override
  public Page<RoleVo> selectRolePage(Page<RoleVo> page) {
    Page<RoleVo> roleVoPage = roleMapper.selectRoleVo(page);
    List<RoleVo> roles = roleVoPage.getRecords();
    for (RoleVo role : roles) {
      List<String> urlList = new ArrayList<>();
      List<Long> longs = actionMapper.selectActionIdByRoleId(role.getId());
      for (Long aLong : longs) {
        Action action = actionMapper.selectById(aLong);
        urlList.add(action.getPatternUrl());
      }
      role.setUrlList(urlList);
    }
    return roleVoPage;
  }
}
